﻿using System;
using System.Collections.Generic;
using System.Linq;
using RanchBuddy.Core.Domain.Impl;
using RanchBuddy.Core.Services.Impl;
using StructureMap;

namespace RanchBuddy.Core.DataAccess.Impl
{
    [Pluggable("Default")]
    public class CompanyRepository : ICompanyRepository
    {
        public IRepositoryStatus GetCompanies(int page, int count, int zipcode, int radius)
        {
            List<Company> result = new List<Company>(); 
            List<int> zips = new List<int>();
            IRepositoryStatus rs = ObjectFactory.GetInstance<IRepositoryStatus>();
            try
            {
                using (RanchBuddyDataContext dc = ObjectFactory.GetInstance<IConnection>().GetLsContext())
                {
                    List<ZipCodesByRadiusResult> results = dc.ZipCodesByRadius(zipcode.ToString(), radius).ToList();
                    zips = results.Select(r => Convert.ToInt32(r.ZipCode)).ToList();
                    result = dc.Companies.Where(c => zips.Contains(Convert.ToInt32(c.ZipCode))).OrderBy(c=>c.CompanyTypeID).ThenBy(c=>c.Name).ToList();
                }
                rs.StatusType = Status.StatusTypes.Success;
                rs.SetObject(result);
            }
            catch (Exception e)
            {
                rs.StatusType = Status.StatusTypes.Failure;
                rs.AddMessage(e.Message + Environment.NewLine + e.StackTrace);
            }
            return rs;
        }

        public IRepositoryStatus GetCompanyByID(Int32 CompanyID)
        {
            IRepositoryStatus rs = ObjectFactory.GetInstance<IRepositoryStatus>();
            Company result = new Company();
            try
            {
                using (RanchBuddyDataContext dc = ObjectFactory.GetInstance<IConnection>().GetLsContext())
                {
                    result = dc.Companies.Where(c => c.CompanyID == CompanyID).FirstOrDefault();
                }
                rs.SetObject(result);
                rs.StatusType = Status.StatusTypes.Success;
            }
            catch(Exception e)
            {
                rs.StatusType = Status.StatusTypes.Failure;
                rs.AddMessage(e.Message + Environment.NewLine + e.StackTrace);
            }
            return rs;
        }

        public IRepositoryStatus SaveCompany(Company company)
        {
            IRepositoryStatus rs = ObjectFactory.GetInstance<IRepositoryStatus>();
            try
            {
                using (RanchBuddyDataContext dc = ObjectFactory.GetInstance<IConnection>().GetLsContext())
                {
                    if (company.CompanyID > 0)
                    {
                        company.UpdateDate = DateTime.Now;
                        dc.Companies.Attach(company, true);
                    }
                    else
                    {
                        company.CreateDate = DateTime.Now;
                        company.UpdateDate = DateTime.Now;
                        dc.Companies.InsertOnSubmit(company);
                    }
                    dc.SubmitChanges();
                }
                rs.SetObject(company);
                rs.StatusType = Status.StatusTypes.Success;
            }
            catch (Exception e)
            {
                rs.SetObject(company);
                rs.StatusType = Status.StatusTypes.Failure;
                rs.AddMessage(e.Message + Environment.NewLine + e.StackTrace);
            }

            return rs;
        }

        public IRepositoryStatus GetCompanyByName(string companyName)
        {
            companyName = companyName.Replace("-", " ");
            IRepositoryStatus rs = ObjectFactory.GetInstance<IRepositoryStatus>();
            Company company = new Company();
            try
            {
                using (RanchBuddyDataContext dc = ObjectFactory.GetInstance<IConnection>().GetLsContext())
                {
                    company = dc.Companies.Where(c => c.Name == companyName).FirstOrDefault();
                    rs.StatusType = Status.StatusTypes.Success;
                    rs.SetObject(company);
                }
            }
            catch (Exception)
            {
                rs.StatusType = Status.StatusTypes.Failure;
            }
            return rs;
        }
    }
}
